package com.lyf.emall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.emall.bean.*;
import com.lyf.emall.mapper.AuthorityMapper;
import com.lyf.emall.mapper.RoleMapper;
import com.lyf.emall.service.RoleService;
import com.lyf.emall.service.SysService;
import com.sun.org.apache.regexp.internal.RE;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/4/8-11:09
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AuthorityMapper authorityMapper;

    @Override
    public ResponseResult getAllRoleInfo(Page page) {
        if (page.getPageNo() < 0){
            new ResponseResult(400, "页码不能为负数");
        }

        List<RoleAndAuthority> roleAndAuthorities = new ArrayList<>();
        // 获得所有的角色信息
        List<SysRole> sysRoles = roleMapper.queryAllRole();

        // 查询对应角色的权限信息
        for (int i = 0 ; i < sysRoles.size() ; i++){
            StringBuilder stringBuilder = new StringBuilder();
            List<SysAuthority> authorityNameByRoleId = authorityMapper.queryAuthorityNameByRoleId(sysRoles.get(i).getRoleId());
            for (SysAuthority s : authorityNameByRoleId){
                stringBuilder.append(s.getAuthorityName()+" | ");
            }

            RoleAndAuthority roleAndAuthority = new RoleAndAuthority(sysRoles.get(i).getRoleId(), sysRoles.get(i).getRoleName(), sysRoles.get(i).getStatus(),sysRoles.get(i).getDelFlag(), sysRoles.get(i).getDescription(), authorityNameByRoleId, stringBuilder.toString());
            roleAndAuthorities.add(roleAndAuthority);
        }

        PageHelper.startPage(page.getPageNo(), page.getPageNum());

        PageInfo<RoleAndAuthority> pageInfo = new PageInfo<>(roleAndAuthorities);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获取角色信息成功",map);
    }

    @Override
    public ResponseResult getAllRoleByAdmin() {
        List<SysRole> sysRoles = roleMapper.queryAllAdmin();
        return new ResponseResult(200, "获取角色信息成功", sysRoles);
    }

    @Transactional
    @Override
    public ResponseResult addRole(SysRole role, Integer[] authorityId) {
        Integer insertAdminInfo = roleMapper.insertAdminInfo(role);
        if (authorityId.length != 0 || authorityId != null){
            for (Integer id : authorityId){
                Integer insertAdminAuthority = roleMapper.insertAdminAuthority(new SysRoleAuthority(role.getRoleId(), id));
            }
        }
        if (insertAdminInfo <= 0){
            return new ResponseResult(400, "添加角色失败");
        }
        return new ResponseResult(200, "添加角色成功");
    }

    @Override
    public ResponseResult updateRoleStatus(Integer roleId, Integer status) {
        Integer updateRoleStatus = roleMapper.updateRoleStatus(roleId, status);
        if (updateRoleStatus <= 0){
            return new ResponseResult(400, "修改角色状态失败");
        }
        return new ResponseResult(200, "修改角色状态成功");
    }

    @Transactional
    @Override
    public ResponseResult updateRoleInfo(SysRole role, Integer[] authorityId) {
        Integer updateRoleInfo = roleMapper.updateRoleInfo(role);
        if (authorityId.length != 0 || authorityId != null){
            Integer deleteRoleAuthority = roleMapper.deleteRoleAuthority(role.getRoleId());
            for (Integer id : authorityId){
                Integer insertAdminAuthority = roleMapper.insertAdminAuthority(new SysRoleAuthority(role.getRoleId(), id));
            }
        }
        if (updateRoleInfo <= 0){
            return new ResponseResult(400, "修改用户信息失败");
        }
        return new ResponseResult(200, "修改用户信息成功");
    }

    @Override
    public ResponseResult isRoleByRoleName(String roleName) {
        if (!StringUtils.hasText(roleName)){
            return new ResponseResult(400, "角色名称不能为空");
        }
        SysRole sysRole = roleMapper.queryRoleByRoleName(roleName);
        if (sysRole != null){
            return new ResponseResult(400, "角色名称重名");
        }
        return new ResponseResult(200, "角色名称可用");
    }
}
